package com.testmanager.testmananger.shuiyou.strCompare.service;

import com.testmanager.testmananger.foundation.util.FlakeIdGenerator;
import com.testmanager.testmananger.shuiyou.strCompare.domain.SyProjectEntity;
import com.testmanager.testmananger.shuiyou.strCompare.dto.SyProjectDto;
import com.testmanager.testmananger.shuiyou.strCompare.mapper.SyProjectMapper;
import com.testmanager.testmananger.shuiyou.strCompare.respository.SyProjectRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


@Slf4j
@Service
public class SyProjectService {

    @Autowired

    SyProjectRepository syProjectRepository;

    @Autowired
    FlakeIdGenerator flakeIdGenerator;
//    public ResponseEntity getSyProject(){
//        log.info("getSyProject");
//        List<SyProjectEntity> all = syProjectRepository.findAll();
//
//        return ResponseEntity.ok(all);
//
//    }

    public ResponseEntity saveOrUpdateSyProject(SyProjectEntity syProjectEntity){
        log.info("saveSyProject:{}",syProjectEntity);

        try {
            if (0 == syProjectEntity.getId()){
                syProjectEntity.setId(flakeIdGenerator.newId());
            }
            SyProjectEntity save = syProjectRepository.save(syProjectEntity);
            SyProjectDto childrenProject = findChildrenProject(save);
            return ResponseEntity.ok(childrenProject);
        }catch (Exception e){
            return ResponseEntity.badRequest().body(e);
        }

    }




    public ResponseEntity getSyProject(){
        log.info("getSyproject");

        List<SyProjectEntity> mainProjects = syProjectRepository.findAllByParentId(0);

        List<SyProjectDto> collect = mainProjects.stream().parallel().map(t -> findChildrenProject(t)).collect(Collectors.toList());

        return ResponseEntity.ok(collect);


    }





    private SyProjectDto findChildrenProject(SyProjectEntity syProjectEntity){

        List<SyProjectEntity> childrens = syProjectRepository.findAllByParentId(syProjectEntity.getId());
        if (childrens.size() ==0){
            return SyProjectMapper.toSyprojectDto(syProjectEntity,new ArrayList<>());
        }

        List<SyProjectDto> collect = childrens.stream().map(t -> findChildrenProject(t)
        ).collect(Collectors.toList());


        return   SyProjectMapper.toSyprojectDto(syProjectEntity,collect);

    }

    public List<Long> findAllChildRenIds(long fatherId){
        log.info("findAllChildRenIds,fatherId:{}",fatherId);
        List<Long>  listId = new ArrayList<>();
        List<SyProjectEntity> allByParentId = syProjectRepository.findAllByParentId(fatherId);
        if (allByParentId.size() == 0){
            listId.add(fatherId);
            return listId;
        }
        allByParentId.stream().forEach(t -> {
            listId.addAll(findAllChildRenIds(t.getId()));
        });
        listId.add(fatherId);
        log.info("=========={}",listId);
        return  listId;

    }



}

